class WebOrder < ActiveRecord::Base
  has_many :tickets, :class_name => 'WebTicket', :foreign_key => 'web_order_id'
  belongs_to :seller, :class_name => 'WebUser', :foreign_key => 'seller_id'
  
  belongs_to :from_airport, :class_name => 'Airport', :foreign_key => 'from_id'
  belongs_to :to_airport, :class_name => 'Airport', :foreign_key => 'to_id'
  
  belongs_to :web_user
  
  def self.saveWebOrder(uid,pnr,airNo,fromto,fly_date,fly_time,arr_time,position,plane_type,link_man,link_tel,link_email,link_address,price,seat,i_number,tktl_date,tktl_time,rake_off)
    begin
      price_sum = price.to_i + CommonHelper.getPlaneTax(plane_type) + CommonHelper.getFuelTax(fromto) + (Constants::DEFAULT_INSURANCE_PRICE*i_number)
      order = WebOrder.new
      order.pnr = pnr if pnr
      order.web_user_id = uid.to_i if uid
      order.price_sum = price_sum*seat.to_i
      order.air_no = airNo if airNo
      if fromto
        order.fromto = fromto
        fromAirport = Airport.find_by_code(fromto[0,3])
        toAirport = Airport.find_by_code(fromto[0,3])
        order.from_id = fromAirport.id
        order.to_id = toAirport.id
      end
      order.fly_date = fly_date if fly_date
      order.fly_time = fly_time if fly_time
      order.arr_time = arr_time if arr_time
      order.position = position if position
      order.plane_type = plane_type if plane_type
      order.link_man = link_man if link_man
      order.link_tel = link_tel if link_tel
      order.link_email = link_email if link_email
      order.link_address = link_address if link_address
      order.insurance_number = i_number.to_i if i_number
      #order.order_request = order_request if order_request
      order.trx_status = Constants::WEB_ORDER_STATUS_DDZF #等待支付
      order.status = Constants::WEB_ORDER_STATUS_DDZF
      #order.status = Constants::ORDER_STATUS_YDD
      order.tktl_date = tktl_date if tktl_date
      order.tktl_time = tktl_time if tktl_time
      order.rake_off = rake_off.to_i if rake_off
      order.order_no = getNewWebOrderNo()
      if order.save
        return order.id
      end
      return nil
    rescue Exception => e
      logger.error("model WebOrder saveWebOrder e="+e.message)
      return nil
    end
  end
  
  def self.getNewWebOrderNo()
    begin
      now = Time.new
      order_no = "T"+now.strftime("%Y%m%d")
      order = WebOrder.find(:first,:conditions=>"order_no is not null and order_no like '"+order_no+"%'",:order=>"id desc")
      if order && order.order_no
        no = order.order_no
        key = no[9,5].to_i+1
        if key<10
          key = "0000"+key.to_s
        elsif key<100
          key = "000"+key.to_s
        elsif key<1000
          key = "00"+key.to_s
        elsif key<10000
          key = "0"+key.to_s
        else
          key = key.to_s
        end
      else
        key = "00001"
      end
      order_no << key
      return order_no
    rescue Exception => e
      logger.error("model WebOrder getNewWebOrderNo e="+e.message)
    end
  end
  
end
